package app.web.hcartoon.collect;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.client.netant.NetAnt;
import cn.lily.com.container.data.Record;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.exception.YaconException;
import cn.lily.esb.enums.EnumManager;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.expand.form.TFileInput;
import cn.lily.web.widget.page.TPanel;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TDrop;
import cn.lily.web.widget.standard.TInput;

/**
 * 动漫明细Edit页面
 * @author JiaJ
 * @date 2018年2月5日
 *
 */
public class HCartoonDetailEdit extends BusinessPage {

	 public void page_onLoad() throws YaconException {

		 this.mainPanel();
		 
		 this.detailPanel();

	 }
	 
	 public void mainPanel(){
		 
		 TPanel HCartoonMainPanel = Panel("HCartoonMainPanel");
		 HCartoonMainPanel.creatToolbar().setBarText("动漫基本信息");

		 Form("HCartoonMainForm").setDictTable("LEO_GALCAN_INFO");
		 
		 TDrop instDrop = new TDrop("INST_DROP");
		 instDrop.setLabel("自建相关信息");	 
		 instDrop.put("0", "否");
		 instDrop.put("1", "是");
		 instDrop.setEditDisabled(true);
		 
		 instDrop.setBackColor("red");
		 instDrop.setFontColor("white");
		 
		 Form("HCartoonMainForm").createFormCell(instDrop);
		 
		 Form("HCartoonMainForm").createFormRow();
		 
		 //爬虫整合
		 TInput httpInput = new TInput("NET_ANT_SITE");
		 httpInput.setLabel("BANGUMI网址");
		 httpInput.setBackColor("green");
		 httpInput.setFontColor("white");
		 	 
		 TButton searchBtn = new TButton("searchBtn");
		 searchBtn.setViewClick("siteAnalyse_click");
		 searchBtn.setLabel("分析");
		 searchBtn.setBackColor("green");
		 searchBtn.setFontColor("white");
		 
		 httpInput.addHintButton(searchBtn);
		 
		 Form("HCartoonMainForm").createFormCell(httpInput);
		 
		 Form("HCartoonMainForm").createFormRow();
		 
		 //动漫名称
		 Form("HCartoonMainForm").createFormCell(Input("CAN_NAME").setEditDisabled(true));
		 		
		 
		 //日文名
		 Form("HCartoonMainForm").createFormCell(Input("CAN_NAM_ENG").setEditDisabled(true));
		 Form("HCartoonMainForm").createFormRow();

		 //英文名
		 Form("HCartoonMainForm").createFormCell(Input("CAN_NAM_JAP").setEditDisabled(true));
		 Form("HCartoonMainForm").createFormRow();
		 
		 //动漫首字母
		 Form("HCartoonMainForm").createFormCell(Input("CAN_FIRST").setEditDisabled(true));
		 Form("HCartoonMainForm").createFormRow();
		 
		 HCartoonMainPanel.createPanelCell(Form("HCartoonMainForm"));
		 
		 getPage().addElementRow(HCartoonMainPanel);
		 
	 }
	 
	 public void detailPanel(){
		 
		 TPanel HCartoonDetailPanel = Panel("HCartoonDetailPanel");
		 HCartoonDetailPanel.creatToolbar().setBarText("动漫明细信息");
		 
		 Form("HCartoonDetailForm").setDictTable("LEO_GALCAN_DETAIL_INFO,LEO_RES_INFO");
		 
		 Form("HCartoonDetailForm").createFormCell(Input("CAN_DETAIL_NAME"));
		 Form("HCartoonDetailForm").createFormRow();
		 

		 Form("HCartoonDetailForm").createFormCell(Drop("CAN_QUA"));
		 
		 TInput timeInput = Input("CAN_TIME_FIX");
		 timeInput.setLabel("上市时间");
		 Form("HCartoonDetailForm").createFormCell(timeInput);
		 
		 Form("HCartoonDetailForm").createFormCell(Drop("CAN_WORD"));
		 
		 Form("HCartoonDetailForm").createFormRow();

		 Form("HCartoonDetailForm").createFormCell(Drop("CAN_GQE"));
		 Form("HCartoonDetailForm").createFormCell(Drop("CAN_VISUAL"));
		 Form("HCartoonDetailForm").createFormCell(Input("CAN_TOTAL"));
		 Form("HCartoonDetailForm").createFormRow();
		 
		 Form("HCartoonDetailForm").createFormCell(Input("CAN_FIRM_MEMO"));		 
		 Form("HCartoonDetailForm").createFormCell(Input("CAN_TAG_MEMO"));
		 Form("HCartoonDetailForm").createFormRow();
		 
		 TDrop diskDrop = Drop("DISK_ID");
		 diskDrop.setLabel("硬盘信息");
		 
		 String state = this.getAttr("state");
		 if("insert".equals(state)){
			 diskDrop.setEnumDetail(EnumManager.getDBEnum("select DISK_ID,DISK_NAM from ${schema}.LEO_DISK_INFO where DISK_STATUS='1' order by DISK_ID DESC"));			 
		 }else{
			 diskDrop.setEnumDetail(EnumManager.getDBEnum("select DISK_ID,DISK_NAM from ${schema}.LEO_DISK_INFO order by DISK_ID DESC"));			 
		 }
		 
		 Form("HCartoonDetailForm").createFormCell(diskDrop,2);
		 
		 Form("HCartoonDetailForm").createFormCell(Input("RES_STORE_SIZE"),2);
		 	 
		 Form("HCartoonDetailForm").createFormCell(Input("RES_STORE_PLACE"),8);
		 Form("HCartoonDetailForm").createFormRow();
		 
		 //简单文件上传
		 TFileInput fileInput = FileInput("TEST_FILE_INPUT");

		 fileInput.setLabel("测试上传");

		 //设置文件类型
		 fileInput.setFileTypes("jpg,gif,png,jpeg");

		 //支持拖拽处理
		 fileInput.setZone(false);

		 //异步上传
		 fileInput.setAsync(true);

		 //webroot下file
		 fileInput.setFileFolder("/file/hcartoon/image");

		 //后续执行类
		 fileInput.setAfterClass("app.web.hcartoon.collect.PictureUpload");

		 //添加其他组件部分
		 fileInput.setComponent(Input("CAN_PATH").setLabel("上传图片"));

		 Form("HCartoonDetailForm").createFormCell(fileInput);
		 Form("HCartoonDetailForm").createFormRow();

		 Form("HCartoonDetailForm").createFormCell(TextArea("CAN_DEMO").setAreaRow(9));
		 Form("HCartoonDetailForm").createFormRow();
		 
		 
		 HCartoonDetailPanel.createPanelCell(Form("HCartoonDetailForm"));
		 
		 HCartoonDetailPanel.creatStatusBar().addElement(Button("save"));
		 HCartoonDetailPanel.getStatusBar().addElement(Button("exit"));
		 
		 getPage().addElementRow(HCartoonDetailPanel);
		 
	 }

	 public void loadData(){
		 String canID = "";
		 if(this.getViewTable().next()){
			 canID = this.getViewTable().getString("canID");
		 }
		 
		 DBManager dbManager = DBFactory.getActionDB();
		 DataTable dt = dbManager.executeQueryTable("select * from ${schema}.LEO_GALCAN_INFO where CAN_ID='"+canID+"'");
		 this.setDataTable(dt);
		 
	 }
	 
	 public void analyseData(){
		 
		 String netSite = "";
		 if(this.getViewTable().next()){
			 netSite = this.getViewTable().getString("NET_ANT_SITE");
			 
			 NetAnt netAnt = new NetAnt(netSite, 2000);
				
			 Document document = netAnt.getDocument();
			 
			 Elements tagPart =  document.select("#subject_detail .subject_tag_section .inner span");			 
			 String tags = "";		 
			 for(int i=0;i<tagPart.size();i++){
				 tags += ","+tagPart.get(i).text();
			 }
			 
			 Elements infoPart = document.select("#columnSubjectHomeA #bangumiInfo #infobox li");
			 String firm = "";
			 for(int i=0;i<infoPart.size();i++){
				 Element active = infoPart.get(i);
				 String title = active.select("span").get(0).text();
				 
				 if(title.startsWith("製作") || title.startsWith("动画制作") || title.startsWith("制作")){
					 
					 Elements firms = active.select("a");
					 if(firms.size()>0){
						 firm = firms.get(0).text();
					 }else{
						 String firmTmp = active.text();
						 firm = firmTmp.substring(firmTmp.indexOf("</span>")+7);
					 }
					 break;
				 }

			 }
			 
			 DataTable dateTable = new DataTable("retTable");
			 dateTable.createColumn("TAGS");
			 dateTable.createColumn("FIRM");
			 
			 Record active = dateTable.createRecord();		 
			 active.setFieldValue("TAGS", tags.substring(1));
			 active.setFieldValue("FIRM", firm);
			 
			 this.setDataTable(dateTable);
			 
		 }else{
			 this.setError("无分析数据！");
		 }
		 
		 
	 }

}
